<template>
  <div style="padding-top: 10px;background-color: #ffffff">
    <div class="filter-container">
      <el-form :inline="true" :model="pagination" class="demo-form-inline" style="margin-right: 1100px;white-space: nowrap">
<!--      <el-form-item label="">-->
<!--        <el-input v-model="pagination.queryString" placeholder="体质分类/中药方剂"></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item>-->
<!--        <el-button type="primary" @click="onSubmit">查询</el-button>-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="warning" @click="addDialogFormVisible">新增</el-button>
      </el-form-item>
      </el-form>
    </div>
    <el-table :data="dataList" :pagination="false" border style="width: 100%" @selection-change="handleSelectionChange">
<!--      <el-table-column type="index" align="center" label="序号"></el-table-column>-->
      <el-table-column prop="constitutionName" label="体质分类" align="center"></el-table-column>
      <el-table-column prop="constitutionDesc" label="体质特点,症状" align="center" width="240px"></el-table-column>
      <el-table-column prop="suitableFood" label="宜食" align="center" width="240px"></el-table-column>
      <el-table-column prop="noSuitableFood" label="忌食" align="center" width="240px"></el-table-column>
      <el-table-column prop="suitableCrowd" label="适宜的人群" align="center" width="240px"></el-table-column>
      <el-table-column prop="dosing" label="剂量" align="center" width="240px"></el-table-column>
      <el-table-column prop="efficacy" label="功效" align="center" width="240px"></el-table-column>
      <el-table-column prop="chineseMedicineName" label="中药方剂" align="center" width="240px"></el-table-column>
      <el-table-column fixed="right" width="180px" label="操作" align="center" >
        <template v-slot="scope">
          <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
          <el-popconfirm
            title="这是一段内容确定删除吗？"
            icon="el-icon-info"
            icon-color="red"
            @confirm="deleteInfo(scope.row)"
            style="margin-left: 5px;white-space: nowrap"
          >
            <el-button slot="reference" type="danger" size="mini">删除</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div style="text-align: right;padding: 10px 20px;" >
      <el-pagination
        @size-change="handlePageSizeChange"
        @current-change="handleCurrentPageChange"
        :current-page="pagination.page"
        :page-sizes="[5, 10, 15, 25, 30]"
        :page-size="pagination.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pagination.total" />
    </div>
    <!-- 新增标签弹层 -->
    <div class="add-form">
      <el-dialog title="新增信息" :visible.sync="dialogFormVisible" :modal="false">
        <el-form ref="formDataList" :model="dataForm" :rules="rules" label-position="right" status-icon label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="体质分类" prop="constitutionName">
                <el-input v-model="dataForm.constitutionName"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="体质特点,症状" prop="constitutionDesc">
                <el-input v-model="dataForm.constitutionDesc"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="宜食" prop="suitableFood">
                <el-input v-model="dataForm.suitableFood"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="忌食" prop="noSuitableFood">
                <el-input v-model="dataForm.noSuitableFood"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="15">
            <el-col :span="12">
              <el-form-item label="适宜人群" prop="suitableCrowd">
                <el-input v-model="dataForm.suitableCrowd"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="剂量" prop="dosing">
                <el-input v-model="dataForm.dosing"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="功效" prop="efficacy">
                <el-input v-model="dataForm.efficacy" type="textarea" :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="中药方剂" prop="chineseMedicineName">
                <el-input v-model="dataForm.chineseMedicineName" type="textarea" :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="handleAdd">确定</el-button>
        </div>
      </el-dialog>
    </div>

    <!-- 编辑标签弹层 -->
    <div class="add-form">
      <el-dialog title="编辑信息" :visible.sync="dialogFormVisible4Edit" :modal="false">
        <el-form ref="dataEditForm" :model="dataForm" :rules="rules" status-icon label-position="right" label-width="100px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="体质分类" prop="constitutionName">
                <el-input v-model="dataForm.constitutionName"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="体质特点,症状" prop="constitutionDesc">
                <el-input v-model="dataForm.constitutionDesc"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="宜食" prop="suitableFood">
                <el-input v-model="dataForm.suitableFood"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="忌食" prop="noSuitableFood">
                <el-input v-model="dataForm.noSuitableFood"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row :gutter="15">
            <el-col :span="12">
              <el-form-item label="适宜人群" prop="suitableCrowd">
                <el-input v-model="dataForm.suitableCrowd"/>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="剂量" prop="dosing">
                <el-input v-model="dataForm.dosing"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="功效" prop="efficacy">
                <el-input v-model="dataForm.efficacy" type="textarea" :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="中药方剂" prop="chineseMedicineName">
                <el-input v-model="dataForm.chineseMedicineName" type="textarea" :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div  slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
          <el-button type="primary" @click="handleEdit()">确定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import request from '@/api/request'
import { health, healthDetailUpdate, healthDetailInsert } from '@/api/health'
export default {
  data () {
    return {
      dataList: [],
      dataForm: {},
      pagination: {
        currentPage: 1,
        pageSize: 5,
        total: 0,
        queryString: ''
      },
      dialogFormVisible: false, // 增加表单是否可见
      dialogFormVisible4Edit: false, // 编辑表单是否可见
      // rules: { // 校验规则
      //   code: [{ required: true, message: '项目编码为必填项', trigger: 'blur' }],
      //   name: [{ required: true, message: '项目名称为必填项', trigger: 'blur' }]
      // }
      rules: {
        constitutionName: [{ required: true, message: '体质分类为必填项', trigger: 'blur' }],
        constitutionDesc: [{ required: true, message: '体质特点,症状为必填项', trigger: 'blur' }],
        suitableFood: [{ required: true, message: '宜食为必填项', trigger: 'blur' }],
        noSuitableFood: [{ required: true, message: '忌食为必填项', trigger: 'blur' }],
        suitableCrowd: [
          { required: true, message: '适宜人群为必填项', trigger: 'blur' }
        ],
        dosing: [{ required: true, message: '剂量为必填项', trigger: 'blur' }],
        efficacy: [{ required: true, message: '功效为必填项', trigger: 'blur' }],
        chineseMedicineName: [{ required: true, message: '常用中药为必填项', trigger: 'blur' }]
      }
    }
  },
  mounted () {
    this.findPage()
  },
  methods: {
    handleSelectionChange () {},
    async findPage () {
      const healthData = {
        currentPage: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
        queryString: this.pagination.queryString
      }
      try {
        await health(healthData).then(resp => {
          this.dataList = resp.data.rows
          this.pagination.total = resp.data.total
        })
      } catch (error) {
        this.handleAxiosError(error)
      }
    },
    onSubmit () {
      this.findPage()
      this.pagination.currentPage = 1
    },
    handleAdd () {
      this.$refs.formDataList.validate((val) => {
        if (val) {
          healthDetailInsert(this.dataForm).then(resp => {
            const data = resp.data
            if (data.flag === true) {
              this.$message({
                type: 'success',
                message: resp.data.message
              })
              this.findPage()
              this.dialogFormVisible = false
            } else {
              this.$message({
                type: 'error',
                message: resp.data.message
              })
            }
          })
        }
      })
    },
    handleEdit () {
      this.$refs.dataEditForm.validate((val) => {
        if (val) {
          healthDetailUpdate(this.dataForm).then(resp => {
            const data = resp.data
            if (data.flag === true) {
              this.$message({
                type: 'success',
                message: resp.data.message
              })
              this.findPage()
              this.dialogFormVisible4Edit = false
            }
          })
        }
      })
    },
    handleUpdate (row) {
      this.dialogFormVisible4Edit = true
      request.get('/identification/info/' + row.id, {
        headers: {
          accept: 'application/json',
          Authorization: 'Bearer ' + localStorage.getItem('token')
        }
      }).then((resp) => {
        if (resp.data.code === 200) {
          this.dataForm = resp.data.data
        }
      })
    },
    deleteInfo (row) {
      const token = localStorage.getItem('token')
      request.delete('/identification/' + row.id, {
        headers: {
          accept: 'application/json',
          Authorization: `Bearer ${token}`
        }
      }).then((resp) => {
        if (resp.data.code === 200) {
          this.$message({
            type: 'success',
            message: resp.data.message
          })
          this.findPage()
        }
      }).catch(err => {
        this.handleAxiosError(err)
      })
    },
    addDialogFormVisible () {
      this.dialogFormVisible = true
      this.formData = []
    },
    handlePageSizeChange (pageSize) {
      this.pagination.pageSize = pageSize
      this.pagination.currentPage = 1
      this.findPage()
    },
    handleCurrentPageChange (currentPage) {
      this.pagination.currentPage = currentPage
      this.findPage()
    },
    handleAxiosError (error) {
      // 检查是否是Axios的网络错误
      if (error.isAxiosError && error.response) {
        // 处理您的网络错误，例如显示用户友好的提示
        this.$message.error('网络请求失败，请稍后重试')
      } else {
        // 处理其他类型的错误
        this.$message.error('网络请求失败，发生错误，请稍后重试')
      }
    }
  }
}
</script>

<style scoped>

</style>
